#!/usr/bin/env python
# Time-stamp: <2010-03-22 23:32:41 Monday by qingant>
# @athor qingant
# @Email qingant@gmail.com
# Lisence:GPL-2


"""
A naive script used to generate org file
"""

import os, re

CONFIG = {
    "pdf":"okular",
    "PDF":"okular",
    "chm":"chmsee",
    "CHM":"chmsee"
}
__PATTERN_S = ".*/([^/]*)\.(.*)$"
__PATTERN = re.compile(__PATTERN_S)

def mywalk(path, visit, arg, depth = 0):
    """ do callback function visit to the filetree rooted at path"""
    #depth = depth
    #print "-----",path,depth,id(depth)
    for fname in os.listdir(path):
        pathname = os.path.join(path, fname)
        if os.path.isdir(pathname):
            visit(arg, pathname, depth)
            mywalk(pathname, visit, arg, depth = depth+1)
        elif os.path.isfile(pathname):
            visit(arg, pathname, depth)
        else :
            pass

def __filter(pathname, config):
    try :
        describe, file_type = __PATTERN.match(pathname).groups()
        return describe, config.get(file_type, None),file_type
    except :
        return os.path.basename(pathname), None,""

def gen_item(_arg1, pathname, depth):

    describe, app, ftype = __filter(pathname, CONFIG)
    depth = "**" + ("*" * depth)
    if app:
        # [[shell:chmsee%20~/apue2.chm%20&][apue]]
        link = "[[shell:%s  %s &][%s]]" % (app, pathname, describe)
        print depth, link
        return
    #(py-execute-def-or-class)
    elif ftype == "html":
        return
    elif ftype == "":
        print depth,"[[file://%s][%s]]" % (pathname, describe)

mywalk("/home/qingant/book", gen_item, "")
